import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: 'Login'
  },
  {
    path: '/Login',
    component: Login,
  },
  {
    path: '/Home',
    component: Home,
    redirect:'/Welcome',
    children:[
      {
        path: '/Welcome',
        component(){
          return import ('../components/Welcome.vue')
        },
      },
      {
        path: '/users',
        component(){
          return import ('../components/user/users.vue')
        }
      },
      {
        path: '/rights',
        component(){
          return import ('../components/Power/rights.vue')
        }
      },
      {
        path: '/roles',
        component(){
          return import ('../components/Power/roles.vue')
        }
      },
      {
        path: '/categories',
        component(){
          return import ('../components/goods/cate.vue')
        }
      },
      {
        path: '/params',
        component(){
          return import ('../components/goods/params.vue')
        }
      },
      {
        path: '/goods',
        component(){
          return import ('../components/goods/list.vue')
        }
      },
      {
        path: '/goods/add',
        component(){
          return import ('../components/goods/add.vue')
        }
      },
      {
        path: '/orders',
        component(){
          return import ('../components/order/order.vue')
        }
      },
      {
        path: '/reports',
        component(){
          return import ('../components/reports/reports.vue')
        }
      },
    ]
  },
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  let token = window.sessionStorage.getItem('token');
  if (to.path === '/Login') {
    return next();
  } else if (!token) {
    return next('/Login');
  }
  next()
})
export default router
